package com.msmall.community.question.action.html;

import java.util.List;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.question.model.Answer;
import com.msmall.community.question.model.Question;
import com.msmall.community.question.service.QuestionManager;
import com.msmall.community.question.util.QuestionUtil;

public class QuestionAction extends HTMLBaseAction{

	private static final long serialVersionUID = -6446124218883189812L;

	private QuestionManager questionManager;
	
	/**
	 * 问题分类id
	 */
	private Long segid;
	
	private Long answerid;
	
	private Long questionid;
	
	/**
	 * ConstantDB.QUESTION_TERMINAL_NO
	 */
	private int terminal;
	
	private Question question;
	
	private Answer answer;
	
	private CascadeCollection<Segment> cascadeSegment;

	public void setQuestionManager(QuestionManager questionManager) {
		this.questionManager = questionManager;
	}
	
	public String execute(){
		return SUCCESS;
	}
	
	/**
	 * 进入提问窗口
	 * @return
	 */
	public String ask(){
		return SUCCESS;
	}
	/**
	 * 保存提问
	 */
	public String saveQuestion(){
		try{
			if(!getIsLogin()){
				message = getText("common.authentication.anonymous");
				return INPUT;
			}
			
			int valid = questionManager.validateQuestionContent(question.getContent());
			if(valid == 2){
				message = getText("html.klg.question.create.content.length.short");
				return INPUT;
			}
			if(valid == 3){
				message = getText("html.klg.question.create.content.length.long");
				return INPUT;
			}
			if(valid == 4){
				message = getText("html.klg.question.create.content.illegal");
				return INPUT;
			}
			//快捷提问
			if(question.getType() == null || question.getType().compareTo(0L)==0){
				question.setType(QuestionUtil.getQuestionDefaultSegment());
			}
			question = questionManager.saveQuestion(question, getUserid(), getUserAgentType());
			questionid = question.getQuestionid();
		}catch(Exception e){
			log.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 查看问题
	 */
	public String viewQuestion(){
		try {
			question = questionManager.incrementQuestionClickAmount(questionid);
			page.setPageSize(30);
			questionManager.getAnswerPaginationByQuestion(page, getUserid(), questionid);
			page.setUrl(ConstantUrl.HTML_KLG_QUESTION_ANSWER_PAGINATION + "?questionid="+ questionid);
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 保存问题回答
	 */
	public String saveAnswer(){
		try{
			boolean success = true;
			StringBuffer buffer = new StringBuffer();
			buffer.append(ConstantAjax.RETURN_PRE);
			if(!getIsLogin()){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("common.authentication.anonymous")));
				success = false;
			}
			if(answer == null && success){
				buffer.append(createResult("false"));
				buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.short")));
				success = false;
			}
			if(success){
				int valid = questionManager.validateAnswerContent(answer.getContent());
				if(valid == 2){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.short")));
					success = false;
				}
				if(valid == 3){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.length.long")));
					success = false;
				}
				if(valid == 4){
					buffer.append(createResult("false"));
					buffer.append(createEasyContent(getText("html.klg.question.answer.create.content.illegal")));
					success = false;
				}
			}
			if(success){
				answer.setNickname(getNickname());
				questionManager.saveQuestionAnswer(answer, getUserid(), getUserAgentType());
				answerid = answer.getAnswerid();
				buffer.append(createResult("true"));
			}
			buffer.append(ConstantAjax.RETURN_SUFF);
			message = buffer.toString();
		}catch(Exception e){
			log.debug("============>", e);
		}
		return SUCCESS;
	}
	/**
	 * 进入问题列表
	 */
	public String list(){
		CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
		if(segid == null || ConstantDB.SEGMENT_ID_QUESTION_NAV.compareTo(segid) < 0){
			segid = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_QUESTION_NAV).getChildren().get(0).getCurrent().getSegmentid();
		}
		cascadeSegment = cs.getAllDatas().get(segid);
		if(cascadeSegment.getCurrent().getParentid().compareTo(ConstantDB.SEGMENT_ID_QUESTION_NAV) != 0){
			List<CascadeCollection<Segment>> parents = cascadeSegment.getParents();
			cascadeSegment = parents.get(0);
		}else{
			segid= cascadeSegment.getChildren().get(0).getCurrent().getSegmentid();
		}
		
		return SUCCESS;
	}

	/**
	 * 获得提问列表
	 */
	public String fetch(){
		try {
			page.setPageSize(3);
			questionManager.getQuestionPagination(page, segid, terminal);
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 删除问答
	 */
	public String delQuestion(){
		try {
			question  = questionManager.delQuestion(getUserid(), questionid);
			segid = question.getType();
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}
	/**
	 * 删除问答回复
	 */
	public String delAnswer(){
		try {
			answer  = questionManager.delAnswer(getUserid(), answerid);
			questionid = answer.getAnswerid();
		} catch (Exception e) {
			log.debug(e);
		}
		return SUCCESS;
	}
	public Long getSegid() {
		return segid;
	}

	public void setSegid(Long segid) {
		this.segid = segid;
	}

	public Long getQuestionid() {
		return questionid;
	}

	public void setQuestionid(Long questionid) {
		this.questionid = questionid;
	}

	public int getTerminal() {
		return terminal;
	}

	public void setTerminal(int terminal) {
		this.terminal = terminal;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public Answer getAnswer() {
		return answer;
	}

	public void setAnswer(Answer answer) {
		this.answer = answer;
	}

	public Long getAnswerid() {
		return answerid;
	}

	public void setAnswerid(Long answerid) {
		this.answerid = answerid;
	}


	public CascadeCollection<Segment> getCascadeSegment() {
		return cascadeSegment;
	}

	public void setCascadeSegment(CascadeCollection<Segment> cascadeSegment) {
		this.cascadeSegment = cascadeSegment;
	}
}
